package com.github.baichuan.mapping;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Optional;

/**
 * EnumDescription映射工具
 */
public class EnumDescriptionMapper {
    public static String description(Enum<?> value){
        Class<? extends Enum> enumClass = value.getDeclaringClass();
        Optional<Field> optionalField = Arrays.asList(enumClass.getDeclaredFields()).stream()
                .filter(x -> x.isEnumConstant() && x.getName().equals(value.name()))
                .findFirst();
        EnumDescription enumDescription = optionalField.get().getAnnotation(EnumDescription.class);
        if(enumDescription != null) {
            return enumDescription.value();
        } else {
            return value.name();
        }
    }

//    public static Map<Integer, String> ordinalMap(Class<? extends Enum> clazz) throws IllegalAccessException {
//        Map<Integer, String> pairs = new TreeMap<>();
//        Set<Field> enumFields = Arrays.stream(clazz.getDeclaredFields()).filter(x -> x.isEnumConstant())
//                .collect(Collectors.toSet());
//        for(Field field : enumFields){
//            EnumDescription description = field.getAnnotation(EnumDescription.class);
//            Enum enumConstant = (Enum)field.get(null);
//            int value = (enumConstant).ordinal();
//            if(description != null) {
//                pairs.put(value, description.value());
//            } else {
//                pairs.put(value, enumConstant.name());
//            }
//        }
//        return pairs;
//    }

//    public static Map<String, String> nameMap(Class<? extends Enum> clazz) throws IllegalAccessException {
//        Map<String, String> pairs = new TreeMap<>();
//        Set<Field> enumFields = Arrays.stream(clazz.getDeclaredFields()).filter(x -> x.isEnumConstant())
//                .collect(Collectors.toSet());
//        for(Field field : enumFields){
//            Enum enumConstant = (Enum)field.get(null);
//            String name = (enumConstant).name();
//            EnumDescription description = field.getAnnotation(EnumDescription.class);
//            if(description != null) {
//                pairs.put(name, description.value());
//            } else {
//                pairs.put(name, name);
//            }
//        }
//        return pairs;
//    }

//    public static List<SelectOption<String>> options(Class<? extends Enum> clazz) throws IllegalAccessException {
//        Set<Field> fields = Arrays.stream(clazz.getDeclaredFields()).filter(x -> x.isEnumConstant())
//                .collect(Collectors.toSet());
//
//        SelectOption<String>[] options = new SelectOption[fields.size()];
//
//        for(Field field : fields){
//            Enum enumConstant = (Enum)field.get(null);
//            String name = (enumConstant).name();
//            int value = enumConstant.ordinal();
//            EnumDescription description = field.getAnnotation(EnumDescription.class);
//            if(description != null) {
//                options[value] = new SelectOption(description.value(), name);
//            } else {
//                options[value] = new SelectOption(name, name);
//            }
//        }
//        return Arrays.asList(options);
//    }
}
